package com.franklin.java.configuration.sql.completion;

import com.franklin.java.api.configuration.AbstractProjectConfigurable;
import com.franklin.java.configuration.sql.completion.ui.CompletionComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * SQL字段提示设置
 * @author Franklin
 * @since 2021/2/19 15:50
 */
public class CompletionConfigurable extends AbstractProjectConfigurable {

    private CompletionComponent completionComponent;

    public CompletionConfigurable(Project myProject) {
        super(myProject);
    }

    @Nls
    @Override
    public String getDisplayName() {
        return "Completion Settings";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        completionComponent = new CompletionComponent(myProject);
        return completionComponent.getMainPanel();
    }

    @Override
    public boolean isModified() {
        boolean isCamelChange = completionComponent.isUseCamel() != CompletionSettingState.isUseCamel(myProject);
        if (isCamelChange){
            return true;
        }
        boolean descriptionChange = !completionComponent.getSelectedDescription().equals(CompletionSettingState.getDescription(myProject));
        if (descriptionChange){
            return true;
        }
        boolean tablePrefixChange = !completionComponent.getTablePrefix().equals(TableNamePrefixState.getPrefix(myProject));
        if (tablePrefixChange){
            return true;
        }
        boolean isShowEntityCommentChange = completionComponent.isShowEntityComment() != CompletionSettingState.isShowEntityComment(myProject);
        if (isShowEntityCommentChange){
            return true;
        }
        boolean colorChange = !completionComponent.getSelectColor().equals(ColorState.getColor(myProject));
        if (colorChange){
            return true;
        }

        return false;
    }

    @Override
    public void apply() throws ConfigurationException {
        CompletionSettingState completionSettingState = CompletionSettingState.getInstance(myProject);
        completionSettingState.isUseCamel = completionComponent.isUseCamel();
        completionSettingState.description = completionComponent.getSelectedDescription();
        completionSettingState.isShowEntityComment = completionComponent.isShowEntityComment();

        TableNamePrefixState tableNamePrefixState = TableNamePrefixState.getInstance(myProject);
        tableNamePrefixState.prefix = completionComponent.getTablePrefix();

        ColorState colorState = ColorState.getInstance(myProject);
        colorState.color = completionComponent.getSelectColor();
    }
}
